home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / util / libs / MMULib.lha / MMULib / Lib_Sources / mu_pool.asm < prev    next >
Encoding:
Assembly Source File  |  1998-10-04  |  14.2 KB  |  419 lines

  1. ;*************************************************************************
  2. ;** mmu.library                                                         **
  3. ;**                                                                     **
  4. ;** a system library for arbitration and control of the MC68K MMUs      **
  5. ;**                                                                     **
  6. ;** © 1998 THOR-Software, Thomas Richter                                **
  7. ;** No commercial use, reassembly, modification without prior, written  **
  8. ;** permission of the authors.                                          **
  9. ;** Including this library in any commercial software REQUIRES a        **
  10. ;** written permission and the payment of a small fee.                  **
  11. ;**                                                                     **
  12. ;** This is an internal header file, do not depend on anything here.    **
  13. ;** Use the official include files.                                     **
  14. ;** Distributed only for the mmu.library development group for private  **
  15. ;** use.                                                                **
  16. ;**                                                                     **
  17. ;**---------------------------------------------------------------------**
  18. ;** Block: Memory pool operations                                       **
  19. ;** Because the exec memory pool functions are bugged and it is not     **
  20. ;** clear whether SetPatch is installed to fix it, we use our own.      **
  21. ;*************************************************************************
  22.  
  23. ;FOLD Include
  24.         include INC:macros.asm
  25.         include inc:exec_lib.asm
  26.         include mu_lib.i
  27.         include mu_alerts.i
  28.         include mu_context.i
  29. ;ENDFOLD
  30. ;FOLD External references
  31.         xref MMUName
  32.         xref AllocAligned
  33. ;ENDFOLD
  34.  
  35.         section main_code,code
  36.  
  37.         machine mc68020
  38.  
  39. ;FOLD MUCreatePool
  40. ;*************************************************
  41. ;** MUCreatePool                                **
  42. ;** d0=ReqMents,d1=puddleSize,d2=puddleThresh   **
  43. ;*************************************************
  44.         xdef MUCreatePool
  45. MUCreatePool:
  46.         saveregs a6
  47.  
  48.         move.l mulib_SysBase(a6),a6
  49.  
  50.         sub.l a0,a0
  51.         cmp.l d2,d1
  52.         bcs.s .failexit         ;puddlesize must be larger
  53.         move.l d0,-(sp)
  54.         moveq #7,d0
  55.         add.l d0,d1
  56.         not.b d0
  57.         and.b d0,d1             ;round puddleSize
  58.         move.l d1,-(sp)
  59.         moveq #1,d1             ;public memory
  60.         moveq #$18,d0
  61.         jsr AllocMem(a6)        ;AllocMem
  62.         move.l (sp)+,d1         ;puddleSize
  63.         move.l (sp)+,a0         ;ReqMents
  64.         tst.l d0
  65.         beq.s .failexitd0
  66.         exg.l d0,a0             ;ReqMents->d0,Ptr->a0
  67.         move.l a0,8(a0)
  68.         addq.l #4,a0
  69.         clr.l (a0)
  70.         move.l a0,-(a0)         ;NewList
  71.         movem.l d0-d2,$c(a0)
  72. .failexit:
  73.         move.l a0,d0
  74. .failexitd0:
  75.         loadregs
  76.         rts
  77. ;ENDFOLD
  78. ;FOLD MUDeletePool
  79. ;*************************************************
  80. ;** MUDeletePool                                **
  81. ;** delete the pool *a0                         **
  82. ;*************************************************
  83.         xdef MUDeletePool
  84. MUDeletePool:
  85.         saveregs a2/a6
  86.  
  87.         move.l mulib_SysBase(a6),a6
  88.  
  89.         move.l a0,d0
  90.         beq.s .nopoolexit
  91.         move.l (a0),a2                  ;get first
  92.         do
  93.          move.l (a2),d2                 ;next entry?
  94.          break.s eq
  95.          move.l a2,a1                   ;if so, take it
  96.          move.l d2,a2                   ;remember next
  97.          jsr FreeVec(a6)                ;release it
  98.         loop.s
  99.         lea -4(a2),a1                   ;get pointer to head
  100.         moveq #$18,d0
  101.         jsr FreeMem(a6)                 ;release head
  102. .nopoolexit:
  103.  
  104.         loadregs
  105.         rts
  106. ;ENDFOLD
  107. ;FOLD MUAllocPooled
  108. ;*************************************************
  109. ;** MUAllocPooled                               **
  110. ;** d0=MemSize                                  **
  111. ;** a0=PoolHeader                               **
  112. ;** d1=ReqMents                                 **
  113. ;*************************************************
  114.         xdef MUAllocPooled
  115. MUAllocPooled:
  116.         saveregs d2-d4/a2-a3/a6
  117.  
  118.         move.l mulib_SysBase(a6),a6
  119.  
  120.         move.l d1,d4                    ;keep ReqMents
  121.         move.l d0,d2                    ;ByteSize->d2
  122.         beq .exit
  123.         move.l a0,d0                    ;Header available ?
  124.         beq .exit
  125.         move.l a0,a2                    ;keep it
  126.  
  127.         cmp.l $14(a2),d2                ;compare with puddlesthres
  128.         bhs.s .allocdirect              ;too big, get directly
  129.         cmp.b 3+$c(a2),d4               ;right type ?
  130.         bne.s .allocdirect              ;no, so get directly
  131.                                         ;allocate pool
  132. .tryagain:
  133.         move.l (a2),d3                  ;*first node
  134.         do
  135.          move.l d3,a3
  136.          move.l (a3),d3                 ;available ?
  137.          beq.s .addpool                 ;no, so add new pool
  138.          tst.l 8(a3)                    ;is this a private memory piece ?
  139.          beq.s .addpool                 ;no, so allocate new
  140.  
  141.          move.l a3,a0
  142.          move.l d2,d0                   ;size to d0
  143.          addq.l #4,d0
  144.          jsr Allocate(a6)               ;allocate private
  145.          tst.l d0
  146.         while.s eq                      ;if not, try next
  147.         move.l d0,a0                    ;*Mem->a0
  148.         addq.l #4,d2                    ;one additional long
  149.                                         ;clear it ?
  150.         btst #$10,d4
  151.         beq.s .noclear
  152.         move.l a0,a1
  153.         move.l d2,d1                    ;size -> d1
  154.         addq.l #7,d1
  155.         lsr.l #3,d1
  156.         subq.l #1,d1                    ;size in double long words
  157.         move.w d1,d0
  158.         swap d1
  159.         moveq #0,d3
  160. .swp1:  move.l d3,(a1)+
  161.         move.l d3,(a1)+
  162.         dbra d0,.swp1
  163.         dbra d1,.swp1
  164. .noclear:
  165.         move.l d2,(a0)+                 ;enter length
  166.         move.l a0,d0
  167.         bra .exit
  168. .allocdirect:
  169.         move.l d2,d0                    ;size -> d0
  170.         addq.l #8,d0
  171.         addq.l #4,d0                    ;+$c
  172.         move.l d4,d1                    ;get length
  173.         jsr AllocVec(a6)                ;get as vector
  174.         tst.l d0
  175.         beq .exit                       ;exit if no memory
  176.         move.l d0,a1                    ;*Mem->a1
  177.         move.l a2,a0                    ;Liste->a0
  178.         addq.l #4,a0
  179.         move.l 4(a0),d0
  180.         move.l a1,4(a0)
  181.         exg.l d0,a0
  182.         movem.l d0/a0,(a1)
  183.         move.l a1,(a0)                  ;AddTail
  184.         addq.l #8,a1                    ;length of the node structure
  185.         clr.l (a1)+                     ;erase it. Private, no pool
  186.         move.l a1,d0                    ;return pointer
  187.         bra.s .exit
  188. .addpool:                               ;add a new pool here
  189.         move.l $10(a2),d0               ;PuddleSize->d0
  190.         moveq #$24,d1
  191.         add.l d1,d0                     ;+Headersize
  192.         move.l $c(a2),d1                ;ReqMents
  193.         jsr AllocVec(a6)
  194.         tst.l d0
  195.         beq.s .exit                     ;exit if no memory
  196.  
  197.         move.l d0,a3                    ;head->a3
  198.         move.l d0,a1
  199.         move.l a2,a0
  200.  
  201.         move.l (a0),d0
  202.         move.l a1,(a0)
  203.         movem.l d0/a0,(a1)
  204.         move.l d0,a0
  205.         move.l a1,4(a0)         ;AddHead
  206.  
  207.         moveq #$a,d0
  208.         move.b d0,8(a3)         ;Type,Pri: auf jeden Fall nicht 0
  209.         move.b d0,9(a3)
  210.         move.l #MMUName,$a(a3)  ;set name: mmu.library
  211.         move.l $c(a2),d1        ;ReqMents
  212.         move.l d1,$e(a3)        ;save as reqments of the pool
  213.         lea $24(a3),a0          ;start of the memory list
  214.         moveq #~7,d1
  215.         move.l a0,d0            ;address ->a0
  216.         and.l d0,d1             ;divisible by 8
  217.         move.l d1,a0
  218.         move.l $10(a2),d0       ;PuddleSize->d0
  219.         move.l a0,$10(a3)       ;*First
  220.         move.l a0,$14(a3)       ;*Lower
  221.         move.l d0,$1c(a3)       ;Free
  222.         clr.l (a0)              ;clear *NextHunk
  223.         move.l d0,4(a0)         ;length
  224.         add.l d0,a0             ;end
  225.         move.l a0,$18(a3)       ;*High
  226.         bra .tryagain
  227. .exit:
  228.         loadregs
  229.         rts
  230. ;ENDFOLD
  231. ;FOLD MUFreePooled
  232. ;*************************************************
  233. ;** MUFreePooled                                **
  234. ;** memory *a1, pool *a0                        **
  235. ;*************************************************
  236.         xdef MUFreePooled
  237. MUFreePooled:
  238.         saveregs a2-a3/a6
  239.  
  240.         move.l a1,d1                    ;no memory, freepooled
  241.         beq .exit
  242.  
  243.         move.l mulib_SysBase(a6),a6
  244.  
  245.         move.l a0,d1
  246.         beq .alert                      ;no pool -> alert
  247.  
  248.         lea -$c(a1),a2                  ;pointer to begin
  249.  
  250.                                         ;get the responsible header
  251.         move.l (a0),d1                  ;*First
  252.         do
  253.          move.l d1,a3
  254.          move.l (a3),d1                 ;*next
  255.          beq.s .alert                   ;not found -> alert
  256.          cmp.l a2,a3                    ;our block ?
  257.          beq.s .freeprivate
  258.          tst.l 8(a3)                    ;do we have a private one ?
  259.          reloop.s eq
  260.          cmp.l $14(a3),a1               ;compare with lower
  261.          reloop.s lo                    ;continue if too small
  262.          cmp.l $18(a3),a1
  263.         while.s hs                      ;or too large
  264.  
  265.         move.l a3,a0
  266.         move.l -(a1),d0                 ;get length
  267.         jsr Deallocate(a6)              ;release
  268.  
  269.         move.l 4(a3),a1                 ;*Pred
  270.         move.l 4(a1),d1                 ;exists ?
  271.         beq.s .nopred
  272.         move.l d1,a0                    ;PredPred->a0
  273.         move.l a3,(a0)                  ;insert me as predicessor of predpred
  274.         move.l a0,4(a3)                 ;my pred is predpred
  275.         move.l a3,4(a1)                 ;I'm pred of my old pred
  276.         move.l (a3),a0                  ;get succ
  277.         move.l a0,(a1)                  ;this is succ of my old succ
  278.         move.l a1,4(a0)                 ;pred is pred of my succ
  279.         move.l a1,(a3)                  ;that's my succ
  280.                                         ;or, in other words, move this
  281.                                         ;node towards the head of the list
  282. .nopred:
  283.         move.l a3,a2                    ;*head -> a2
  284.  
  285.         move.l $1c(a2),d0               ;get free mem
  286.         add.l $14(a2),d0                ;plus lower
  287.         cmp.l $18(a2),d0                ;-Upper =0 -> this block is FREE
  288.         bne.s .exit
  289. .freeprivate:                           ;release a complete pool
  290.         move.l a2,a1                    ;to a1
  291.         move.l a1,d0
  292.         move.l (a1)+,a0                 ;next->a0
  293.         move.l (a1),a1                  ;pred->a1
  294.         move.l a0,(a1)
  295.         move.l a1,4(a0)                 ;remove
  296.         move.l a2,a1
  297.         jsr FreeVec(a6)                 ;and release
  298.         bra.s .exit
  299. .alert:
  300.         saveregs a5/d7
  301.         move.l #AN_BadFreePool,d7
  302.         sub.l a5,a5
  303.         jsr Alert(a6)                   ;and alert
  304.         loadregs
  305. .exit:
  306.         moveq #0,d0
  307.         loadregs
  308.         rts
  309. ;ENDFOLD
  310.  
  311. ;FOLD AllocContextMem
  312. ;*************************************************
  313. ;** AllocContextMem                             **
  314. ;** allocate from the context pool d0 bytes     **
  315. ;*************************************************
  316.         xdef AllocContextMem
  317. AllocContextMem:
  318.         move.l a6,a1
  319.         move.l mulib_SysBase(a6),a6
  320.         lea mulib_PoolSemaphore(a1),a0
  321.         jsr ObtainSemaphore(a6)                 ;does not alter registers
  322.  
  323.         move.l a1,a6
  324.         move.l #$10001,d1                       ;Clear it
  325.         move.l mulib_ContextPool(a6),a0
  326.         bsr MUAllocPooled
  327.  
  328.         move.l a6,a1
  329.         move.l mulib_SysBase(a6),a6
  330.         lea mulib_PoolSemaphore(a1),a0
  331.         jsr ReleaseSemaphore(a6)
  332.  
  333.         move.l a1,a6
  334.         tst.l d0
  335.         rts
  336. ;ENDFOLD
  337. ;FOLD FreeContextMem
  338. ;*************************************************
  339. ;** FreeContextMem                              **
  340. ;** release the allocated memory at *a1         **
  341. ;** size is implicit                            **
  342. ;*************************************************
  343.         xdef FreeContextMem
  344. FreeContextMem:
  345.         move.l a1,d0
  346.         move.l a6,a1
  347.         move.l mulib_SysBase(a6),a6
  348.         lea mulib_PoolSemaphore(a1),a0
  349.         jsr ObtainSemaphore(a6)                 ;does not alter registers
  350.  
  351.         move.l a1,a6
  352.         move.l mulib_ContextPool(a6),a0
  353.         move.l d0,a1
  354.         bsr MUFreePooled
  355.  
  356.         move.l a6,a1
  357.         move.l mulib_SysBase(a6),a6
  358.         lea mulib_PoolSemaphore(a1),a0
  359.         jsr ReleaseSemaphore(a6)
  360.  
  361.         move.l a1,a6
  362.         rts
  363. ;ENDFOLD
  364.  
  365. ;FOLD AllocPointerTable
  366. ;*************************************************
  367. ;** AllocPointerTable                           **
  368. ;** allocate a pointer table for the level d0   **
  369. ;** of the MMU table                            **
  370. ;** *a5=Context                                 **
  371. ;**                                             **
  372. ;**---------------------------------------------**
  373. ;** this is currently a stupid stubs for        **
  374. ;** AllocAbs. A future version should handle a  **
  375. ;** pool with cache-inhibited memory            **
  376. ;*************************************************
  377.         xdef AllocPointerTable
  378. AllocPointerTable:
  379.         tst.b mulib_MMUType(a6)                 ;no MMU, no table
  380.         beq.s .failexit
  381.  
  382.         move.b ctx_LevelATableSz(a5,d0.w*4),d1          ;#of bits
  383.         move.l ctx_LevelAAlign(a5,d0.w*4),a0            ;get required alignment
  384.         lsl.l #3,d1                                     ;plus the table again for the user data
  385.  
  386.         move.l d1,d0
  387.         moveq #1,d1                                     ;memory type does not care
  388.         bsr AllocAligned
  389.         tst.l d0
  390.         rts
  391. .failexit:
  392.         moveq #0,d0
  393.         rts
  394. ;ENDFOLD
  395. ;FOLD FreePointerTable
  396. ;*************************************************
  397. ;** FreePointerTable                            **
  398. ;** free a pointer table for the level d0       **
  399. ;** at *a1                                      **
  400. ;*************************************************
  401.         xdef FreePointerTable
  402. FreePointerTable:
  403.         move.l a1,d1
  404.         beq.s .exit                             ;ignore silently
  405.  
  406.         move.l ctx_LevelATableSz(a5,d0.w*4),d0
  407.  
  408.         saveregs a6
  409.  
  410.         movem.l mulib_SysBase(a6),a6
  411.         lsl.l #3,d0
  412.         jsr FreeMem(a6)
  413.  
  414.         loadregs
  415. .exit:
  416.         rts
  417. ;ENDFOLD
  418.  
  419.